package com.ruoyi.home.api;
import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisUtil;
import com.ruoyi.common.utils.ProjectConstant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.aliyun.ThroughUtil;
import com.ruoyi.home.pojo.AppWithdrawalVO;
import com.ruoyi.home.service.AppHomeService;

import io.swagger.annotations.Api;

@RestController
@RequestMapping("/api/v1/home")
@Api(tags = "【APP】首页相关接口") 
@CrossOrigin
public class AppHomeController {

	@Autowired
	private AppHomeService appHomeService;
	@Autowired
	private RedisUtil redisUtil;
	
	@GetMapping(value = "/redisTest")
	public AjaxResult redisTest(Long userId,Integer type){
		String key = ProjectConstant.REDIS_MSG_PREFIX;
		Object ret = new Object();
		switch (type) {
		case 1:
			ret = redisUtil.sSet(key, userId);
			break;
		case 2:
			ret = redisUtil.sHasKey(key, userId);
			break;
		case 3:
			ret = redisUtil.setRemove(key, userId);
			break;
		case 4:
			ret = redisUtil.incr(ProjectConstant.REDIS_MSG_PREFIX+":"+userId, 1);
		default:
			break;
		}
		return AjaxResult.success("成功",ret);
	}
	
	
	
	/**
	 * 	首页用户精选
	 * @param nickName		用户昵称
	 * @param lable			标签
	 * @param pageNo		
	 * @return
	 */
	@GetMapping(value = "/homeUserChoose")
	public AjaxResult homeUserChoose(String nickName,String lable,String longitude,String latitude,Integer pageNo) {
		if(StringUtils.isEmpty(longitude) || StringUtils.isEmpty(latitude)) {
			//return AjaxResult.error(500181,"请开启定位");
			longitude = "112";
			latitude = "28";
		}
		if(null == pageNo) pageNo = 1;
		return appHomeService.homeUserChoose(nickName,lable,longitude,latitude,pageNo);
	}
	
	
	
	/**
	 * 	首页用户数据
	 * @param sex			性别 男=M 女=F
	 * @param pageNo		开始页
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/homeUserList")
	public AjaxResult homeUserList(String sex,String longitude,String latitude,Integer pageNo,String city,String nickName,HttpServletRequest request) {
		if(null == pageNo) {
			pageNo = 1;
		}
		if(StringUtils.isEmpty(longitude) || StringUtils.isEmpty(latitude)) {
			//return AjaxResult.error(500181,"请开启定位");
			longitude = "112";
			latitude = "28";
		}
		System.out.println("首页获取的经度："+longitude);
		System.out.println("首页获取的纬度："+latitude);
		if(StringUtils.isNotEmpty(city)) {
			city = city.replace("市", "");
		}
		return appHomeService.homeUserList(sex, longitude,latitude,pageNo, city,nickName,request);
	}
	
	/**
	 * 	首页用户搜索
	 * @param nickName
	 * @param pageNo
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/homeUserSearch")
	public AjaxResult homeUserSearch(String nickName,Integer pageNo,HttpServletRequest request) {
		if(null == pageNo) {
			pageNo = 1;
		}
		return appHomeService.homeUserSearch(nickName, pageNo, request);
	}
	
	
	
	/**
	 * 	申请提现
	 * @param type				1=收入
	 * @param withdrawAmount	提现金额
	 * @param withdrawTo		提现方式 1=微信 2=支付宝
	 * @return
	 */
	@PostMapping(value = "/doWithdrawal")
	public AjaxResult doWithdrawal(@RequestBody AppWithdrawalVO appWithdrawal) {
		if(null == appWithdrawal) {
			return AjaxResult.error("参数为空");
		}
		if(null == appWithdrawal.getWithdrawTo()) {
			return AjaxResult.error("请选择提现方式");
		}
		if(null == appWithdrawal.getWithdrawAmount()) {
			return AjaxResult.error("请选择提现金额");
		}
		if(null == appWithdrawal.getType()) {appWithdrawal.setType(1);}
		
		if(appWithdrawal.getWithdrawAmount().compareTo(new BigDecimal("1")) == -1) {
			return AjaxResult.error("提现无效，请输入大于1金币的可提现金额");
		}
		if(!ThroughUtil.compareNumber(appWithdrawal.getWithdrawAmount())) {
			return AjaxResult.error("提现无效，请勿输入非整数提现金额");
		}
		return appHomeService.doWithdrawal(appWithdrawal);
	}
	
	/**
	 * 	我的认证状态
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/getMyAuthStatus")
	public AjaxResult getMyAuthStatus(HttpServletRequest request) {
		return appHomeService.getMyAuthStatus(request);
	}
	
	/**
	 * 	获取问答结果
	 * @param type	1=卡牌问答 2=骰子问答 3=星盘问答 4=六爻
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/getQAResults")
	public AjaxResult getQAResults(Integer type,HttpServletRequest request) {
		if(null == type) {
			return AjaxResult.error("请选择问答类型");
		}
		return appHomeService.getQAResults(type,request);
	}
	
	/**
	 * 	排行榜
	 * @param pageNo
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/getRankingList")
	public AjaxResult getRankingList(Integer pageNo,HttpServletRequest request) {
		if(null == pageNo) {
			pageNo = 1;
		}
		return appHomeService.getRankingList(pageNo,request);
	}
	
	/**
	 * 	我的服务
	 * @param type		1=已完成 2=未完成
	 * @return
	 */
	@GetMapping(value = "/myServices")
	public AjaxResult myServices(Integer type) {
		if(null == type || type > 2) {
			type = 1;
		}
		return appHomeService.myServices(type);
	}


	/**
	 * 	我的服务详情
	 * @param id		业务ID
	 * @param type		2=连麦数据 3=活动结束
	 * 1=问答 2=连麦 3=活动 4=咨询项目
	 * @return
	 */
	@GetMapping(value = "/myServicesDetail")
	public AjaxResult myServicesDetail(Long id,Integer type) {
		if(null == type || type > 3 || type < 2) {
			type = 2;
		}
		return appHomeService.myServicesDetail(id,type);
	}
}
